<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\RedBag
 *
 * @property int $id
 * @property int $higher_uid 上级id 分享人id 红包收益者
 * @property int $lower_uid 下级id 应邀人id  红包生产者
 * @property float $total_amount 红包金额
 * @property \Carbon\Carbon $created_at 领取时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RedBag whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RedBag whereHigherUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RedBag whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RedBag whereLowerUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RedBag whereTotalAmount($value)
 * @mixin \Eloquent
 */
class RedBag extends Model
{
    public $table = 'red_bag';
    public static function getRedBagList($key,$val)
    {
        if (!empty($key)&&!empty($val)){
            $data = self::where($key,$val)->get();
        }else{
            $data = self::join('user as u1','u1.id','=','red_bag.higher_uid')
                        ->join('user as u2','u2.id','=','red_bag.lower_uid')
                        ->select('u1.user_name as higher_user_name','u1.user_nickname as higher_user_nickname','u2.user_name as lower_user_name','u2.user_nickname as lower_user_nickname','red_bag.*')
                        ->get();
        }
        if (!empty($data)){
            $data = $data->toArray();
        }else{
            $data = [];
        }
        return $data;
    }

    public static function getRedBagCount()
    {
        $count = $total = 0;
        $count = self::count();
        $total = self::selectRaw('sum(total_amount) as total')->first()->toArray();
        $total = $total['total'] ?? 0;
        $count = $count ?? 0;
        return ['count'=>$count,'total'=>$total];
    }
}
